﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;

public class ButtonBind : MonoBehaviour {

	// Use this for initialization
	void Start () {	
		//Dictionary <int, string> dic = new Dictionary<int, string> () {
		//	{ 0, "aaa"}, { 1, "bbbb"}
		//};

		//foreach (KeyValuePair <int, string> k in dic) {
		//	Debug.Log ("k:" + k.Key + " v:" + k.Value);
		//}

		//KeyValuePair<int, string> mRef = new KeyValuePair<int, string>();
		//foreach (KeyValuePair <int, string> k in dic) {
		//	if (k.Value == "aaa") mRef = k;
		//}
		//if (mRef.Value == "aaa") {
		//	dic.Remove (mRef.Key);
		//}

		//foreach (KeyValuePair <int, string> k in dic) {
		//	Debug.Log ("k:" + k.Key + " v:" + k.Value);
		//}
		
		//JsonData json = new JsonData ();
		//json["Command"] = "KEY_QUERY";
		//json["MAC"] = "00:92:c6:14:7b:7a";
		//json["OrderID"] = "0100200200";
		//json["DeviceName"] = "aceband-X6-0611";

		//Debug.Log (json.ToJson ());

		//Dictionary <string, string> post = new Dictionary<string, string>();
		//post.Add("EncObject", json.ToJson());

		//string url = @"http://www.sencatech.net/reg_ble_register/work/index.php";
		//StartCoroutine (POST (url, post, (isSuccess, result) => {
		//	Debug.Log(result);	

		//	if (isSuccess) {
		//		/// 这里很诡异的一件事件,有错JsonException: Invalid character '﻿' in input string
		//		/// 看起来返回的字符串是{"Error":"MAC_INVALID","Key":""}
		//		/// 其实是&#xFEFF;{"Error":"MAC_INVALID","Key":""}
		//		string msg = result.Substring(result.IndexOf("{"));
		//		JsonData e = JsonMapper.ToObject (msg);
		//		Debug.Log ("Error:" + e["Error"]);
		//		Debug.Log ("Key:" + e["Key"]);
		//	}
		
		//}));
		
		// Http操作
		JsonData json = new JsonData();
		json["Command"] = "KEY_INSERT";
		json["DeviceId"] = "00:92:c6:14:7b:76";
		json["QrId"] = "5486787545";

		Dictionary<string, string> post = new Dictionary<string, string>();
		post.Add("EncObject", json.ToJson());

		string url = @"http://sencatech.net/reg_qr_register/work/index.php";
		StartCoroutine(POST(url, post, (isSuccess, result) => {
			Debug.Log(result);
			if (isSuccess) {
				/// 这里很诡异的一件事件,有错JsonException: Invalid character '﻿' in input string
				/// 看起来返回的字符串是{"Error":"MAC_INVALID","Key":""}
				/// 其实是&#xFEFF;{"Error":"MAC_INVALID","Key":""}
				string msg = result.Substring(result.IndexOf("{"));
				JsonData e = JsonMapper.ToObject(msg);
				/// Debug.Log("Error:" + e["Error"]);
				/// Debug.Log("Key:" + e["Key"]);

				string Key = e["Key"].ToString(); 
				string Error = e["Error"].ToString();
				Debug.Log ("Error:" + Error + " Key:" + Key);
			} else {
				Debug.Log ("Net:" + result);
			}
		}));
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	//POST请求
	IEnumerator POST(string url, Dictionary<string, string> post, System.Action <bool, string> result) {
		WWWForm form = new WWWForm();

		foreach (KeyValuePair<string, string> post_arg in post) {
			form.AddField(post_arg.Key, post_arg.Value);
		}

		WWW www = new WWW(url, form);
		yield return www;
				
		if (www.error != null) {
			//POST请求失败
			//Debug.Log("error is :" + www.error);
			result (false, www.error);
		} else {
			//POST请求成功
			//Debug.Log("request ok : " + www.text);
			result (true, www.text);
		}
	}
}
